<?php

namespace app\api\controller\user;

use app\common\controller\Api;
use think\Db;


class Address extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 收货地址列表
     * @ApiMethod (GET)
     * @path api/user/address/index
     */
    public function index()
    {
        $userId = $this->auth->id;//后期从token或者公共存储区域读取
        $storeId = $this->auth->store_id;//后期从token或者公共存储区域读取
        $addressId = $this->request->param('address_id','');
        $where = ['user_id'=>$userId];
        if (!empty($addressId)) {
            $where['id'] = $addressId;
        }
        $address = db('address')->where($where)->select();
        foreach ($address AS $key => $value) {
            $address[$key]['province_name'] = db('area')->where('id',$value['province'])->value('name');
            $address[$key]['city_name'] = db('area')->where('id',$value['city'])->value('name');
            $address[$key]['area_name'] = db('area')->where('id',$value['area'])->value('name');
        }
        $backList = [
            'address'=>$address
        ];
        $this->success('操作成功',$backList);
    }

    /**
     * 新增收货地址
     * @ApiMethod (GET)
     * @path api/user/address/add
     */
    public function add()
    {
        $storeId = $this->auth->store_id;//后期从token或者公共存储区域读取
        $userId = $this->auth->id;;//后期从token或者公共存储区域读取
        $param = $this->request->post();
        $name = $param['name'];
        $tel = $param['tel'];
        $iSdefault = $this->request->post('is_default',1);
        $address = $param['address'];
        $province = $param['province'];
        $city = $param['city'];
        $area = $param['area'];
        if (empty($name) || empty($tel) || empty($address) || empty($province) || empty($city) || empty($area)) {
            $this->success('缺少参数');
        }
        $param['user_id'] = $userId;
        $param['add_time'] = time();
        if ($iSdefault == 1) {
            db('address')->where('user_id',$userId)->update([
                'is_default'=>0
            ]);
        }
        db('address')->insert($param);
        $this->success('操作成功');
    }

    /**
     * 编辑收货地址
     * @ApiMethod (GET)
     * @path api/user/address/edit
     */
    public function edit()
    {
        $storeId = $this->auth->store_id;//后期从token或者公共存储区域读取
        $userId = $this->auth->id;;//后期从token或者公共存储区域读取
        $param = $this->request->post();
        $id = $param['id'];
        $name = $param['name'];
        $tel = $param['tel'];
        $iSdefault = $this->request->post('is_default',1);
        $address = $param['address'];
        $province = $param['province'];
        $city = $param['city'];
        $area = $param['area'];
        if (empty($name) || empty($tel) || empty($address) || empty($province) || empty($city) || empty($area)) {
            $this->success('缺少参数');
        }
        $param['user_id'] = $userId;
        $param['add_time'] = time();
        if ($iSdefault == 1) {
            db('address')->where('user_id',$userId)->update([
                'is_default'=>0
            ]);
        }
        db('address')->where('id',$id)->update($param);
        $this->success('操作成功');
    }

    /**
     * 删除收货地址
     * @ApiMethod (GET)
     * @path api/user/address/delete
     */
    public function delete()
    {
        $storeId = $this->auth->store_id;//后期从token或者公共存储区域读取
        $userId = $this->auth->id;;//后期从token或者公共存储区域读取
        $param = $this->request->post();
        $id = $param['id'];
        if (empty($id)) {
            $this->success('缺少参数');
        }
        db('address')->where(['id'=>$id,'user_id'=>$userId])->delete();
        $this->success('操作成功');
    }

    /**
     * 设为默认收货地址
     * @ApiMethod (GET)
     * @path api/user/address/setDefault
     */
    public function setDefault()
    {
        $storeId = $this->auth->store_id;//后期从token或者公共存储区域读取
        $userId = $this->auth->id;;//后期从token或者公共存储区域读取
        $id = $this->request->post('id');
        if (empty($id)) {
            $this->success('缺少参数');
        }
        db('address')->where(['user_id'=>$userId])->update(['is_default'=>0]);
        db('address')->where(['id'=>$id,'user_id'=>$userId])->update(['is_default'=>1]);
        $this->success('设置成功');
    }

    /**
     * 收货地址三级联动
     * @ApiMethod (GET)
     * @path api/user/address/area
     */
    /**
     * 读取省市区数据,联动列表
     */
    public function area()
    {
        $params = $this->request->get();
        if (!empty($params)) {
            $province = isset($params['province']) ? $params['province'] : null;
            $city = isset($params['city']) ? $params['city'] : null;
        } else {
            $province = $this->request->get('province');
            $city = $this->request->get('city');
        }
        $where = ['pid' => 0, 'level' => 1];
        $provincelist = null;
        if ($province !== null) {
            $where['pid'] = $province;
            $where['level'] = 2;
            if ($city !== null) {
                $where['pid'] = $city;
                $where['level'] = 3;
            }
        }
        $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
        $this->success('操作成功',$provincelist);
    }
}
